package cn.jxau.web.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.plugins.Page;

import cn.jxau.auth.annotation.AccessControl;
import cn.jxau.auth.annotation.PrivilegeAnnotation;
import cn.jxau.bean.po.Article;
import cn.jxau.bean.po.ArticleComment;
import cn.jxau.bean.po.ArticleType;
import cn.jxau.bean.po.Reply;
import cn.jxau.bean.po.User;
import cn.jxau.bean.po.UserCollectionsType;
import cn.jxau.bean.vo.ArticlePlus;
import cn.jxau.bean.vo.JsonResult;
import cn.jxau.common.util.BaseFileRelativeUtil;
import cn.jxau.common.util.ProUtils;

/**
 * 跟文章相关的请求处理
 * @author lpy
 *
 */
@Controller
@RequestMapping("article")
public class ArticleController extends BaseController{
	@RequestMapping("/articleList")
	@ResponseBody
	public JsonResult articleList(Page<Article> page){
		JsonResult result=null;
		List<Article> list=articleService.getArticleListByPage(page);
		if(list!=null&&list.size()>0){
			result=renderSuccess("获取列表成功!");
			result.addData("lists", list);
		}else{
			result=renderError("文章列表为空或者发生服务器错误");
		}
		result.addData("page", page);
		return result;
	}
	
	@RequestMapping("/typeid")
	@ResponseBody
	public JsonResult getArticleByTypeID(Page<Article> page,Long typeID){
		JsonResult result=null;
		List<Article> list=articleService.getArticleByTypeID(page,typeID);
		if(list!=null&&list.size()>0){
			result=renderSuccess("获取列表成功!");
		}else{
			result=renderError("文章列表为空或者发生服务器错误");
		}
		result.addData("lists", list);
		result.addData("page", page);
		return result;
	}
	
	@RequestMapping("/parentid")
	@ResponseBody
	public JsonResult getArticleByParentID(Page<Article> page,Long parentID){
		JsonResult result=null;
		List<Article> list=articleService.getArticleByParentID(page,parentID);
		if(list!=null&&list.size()>0){
			result=renderSuccess("获取列表成功!");
		}else{
			result=renderError("文章列表为空或者发生服务器错误");
		}
		result.addData("lists", list);
		result.addData("page", page);
		return result;
	}
	
	@RequestMapping("/detail")
	public ModelAndView getArticleDetail(Long id,Long pid,HttpSession session){
		ModelAndView mv = new ModelAndView();
		JsonResult result=null;
		List<Reply> replies = null;
		Article article=articleService.getArticleDetail(id);	//文章详细信息 id：文章id
		User author = articleService.getAuthor(article.getAuthorID());	//作者信息
		User user = (User) session.getAttribute("user"); //登录用户信息
		List<Article> articles = articleService.getFiveArticle(article.getAuthorID()); //右侧五条文章显示
		List<ArticleComment> rootcomments = articleCommentService.getFiveRootComment(id); //五条根节点评论
		if(pid==null){
			replies = replyService.getFirstReply(rootcomments); //每个父节点的一条评论
		}else{
			replies = replyService.getOneAllOtherFirstReply(rootcomments,pid);
			System.out.println(replies);
		}
		try {
			String content = BaseFileRelativeUtil.readInFile(ProUtils.getProperty("text.save")+"/"+article.getPath()+".txt","utf-8");
			content=content.replaceAll("<p><br></p>","");
			System.out.println(content);
			if(article!=null&&author!=null&&content!=null){
				result=renderSuccess("获取详情页信息成功!");
				result.addData("article", article);
				result.addData("author", author);
				result.addData("content", content);
				
				if(articles!=null&&articles.size()>0){
					result.addData("articles", articles);
				}
				if(replies!=null&&replies.size()>0){
					result.addData("replies", replies);
				}
				if(rootcomments!=null&&rootcomments.size()>0){
					result.addData("rootcomments", rootcomments);
				}
				if(user!=null){
					List<UserCollectionsType> typeList = collectionsService.getUserArticleType(user.getId()); //模态框内的收藏
					if(typeList!=null&&typeList.size()>0){
						result.addData("typeList", typeList);
					}
				}
			}else{
				result=renderError("文章为空或者发生服务器错误");
			}
		} catch (Exception e) {
			result=renderError("文章为空或者发生服务器错误");
			e.printStackTrace();
		}
		
		mv.setViewName("detail");
		mv.addObject("result", result);
		return mv;
	}
	
	@RequestMapping("/uparticle")
	public ModelAndView uparticle(){
		ModelAndView mv = new ModelAndView();
		JsonResult result=null;
		List<ArticleType> articleTypes = articleTypeService.getAllArticleType();
			
		if(articleTypes!=null&&articleTypes.size()>0){
			result=renderSuccess("获取上传页信息成功!");
			result.addData("articleTypes", articleTypes);
		}else{
			result=renderError("信息为空或者发生服务器错误");
		}
		mv.setViewName("/uparticle");
		mv.addObject("result", result);
		return mv;
	}
	
	@PrivilegeAnnotation(AccessControl.USER)
	@RequestMapping("/insertComment")
	@ResponseBody
	public JsonResult insertComment(ArticleComment articleComment){
		JsonResult result=null;
		
		if(articleCommentService.insertComment(articleComment)>0){
			result=renderSuccess("添加评论成功!");
		}else{
			result=renderError("添加评论失败或者发生服务器错误");
		}
		return result;
	}
	
	@RequestMapping("/reply")
	@ResponseBody
	public JsonResult insertReply(Reply reply,HttpSession session){
		JsonResult result=null;
		if(replyService.insertReply(reply)>0){
			result=renderSuccess("添加回复成功!");
		}else{
			result=renderError("添加回复失败或者发生服务器错误");
		}
		return result;
	}
	
	@RequestMapping("userArticle")
	@ResponseBody
	public JsonResult getUserArticleList(Page<Article> page,HttpSession session){
		JsonResult result=null;
		User user = (User) session.getAttribute("user");
		if(user!=null){
			List<ArticlePlus> list=articleService.getUserArticleList(page,user.getId());
			if(list!=null&&list.size()>0){
				result=renderSuccess("获取列表成功!");
				result.addData("list", list);
			}else{
				result=renderError("文章列表为空或者发生服务器错误");
			}
			result.addData("page", page);
		}else{
			result=renderError("文章列表为空或者发生服务器错误");
		}
		return result;
	}
	
	@RequestMapping("showContent")
	@ResponseBody
	public JsonResult getUserArticleContent(Long articleID){
		JsonResult result=null;
		Article article =articleService.getArticleDetail(articleID);
		if(article!=null){
			String content = BaseFileRelativeUtil.readInFile(ProUtils.getProperty("text.save")+"/"+article.getPath()+".txt","utf-8");
			if(content!=null){
				result=renderSuccess("获取文章内容成功!");
				result.addData("content", content);
			}
		}else{
			result=renderError("文章不存在或者发生服务器错误");
		}
		return result;
	}
	
	@RequestMapping("deleteUserArticle")
	@ResponseBody
	public JsonResult deleteUserArticle(Long articleID){
		JsonResult result = renderSuccess("响应成功");
		if(articleService.deleteUserArticle(articleID)>0){
			result.addData("reply", 1);
		}else{
			result.addData("reply", 0);
		}
		return result;
	}
	
	
}




















